package nsu.grandfinal;

import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.StrictMode;
import android.util.Log;

public class DaumParser {
	/**
	 * @uml.property  name="m_geoData"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="nsu.grandfinal.GeoData"
	 */
	ArrayList<GeoData>	m_geoData;
	
	public DaumParser() {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
	}
	
	public ArrayList<GeoData> getGeoData(String x, String y) {
		m_geoData = new ArrayList<GeoData>();
		GeoData geoData = null;
		String API_KEY = "ba30922292abc82bc11428704716c3e7f483b89f";
		try {
			URL text = new URL("http://apis.daum.net/local/geo/transcoord?apikey=" + API_KEY + "&x=" + x + "&y=" + y + "&fromCoord=KTM&toCoord=WGS84&output=xml");
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();
			int parserEvent = parser.getEventType();
			Log.i("getPoint", x);
			Log.i("getPoint", y);
			parser.setInput(text.openStream(), null);
			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
					case XmlPullParser.START_TAG:
						String tag = parser.getName();
						Log.i("tag", tag);
						if (tag.compareTo("result") == 0) {
							geoData = new GeoData();
							String xSrc = parser.getAttributeValue(null, "x");// parser.nextText( );
							double dxSrc = Double.valueOf(xSrc).doubleValue();
							Log.i("dxSrc", dxSrc + "");
							geoData.d_dx = dxSrc;
							String ySrc = parser.getAttributeValue(null, "y");// parser.nextText( );
							double dySrc = Double.valueOf(ySrc).doubleValue();
							Log.i("dySrc", dySrc + "");
							geoData.d_dy = dySrc;
							m_geoData.add(geoData);
						}
						if (tag.compareTo("code") == 0) {
							String code = parser.nextText();
							Log.i("Code", code);
						}
						if (tag.compareTo("dcode") == 0) {
							String dcode = parser.nextText();
							Log.i("dcode", dcode);
						}
						if (tag.compareTo("dmessage") == 0) {
							String dmessage = parser.nextText();
							Log.i("dmessage", dmessage);
						}
						break;
				}
				parserEvent = parser.next();
			}
		} catch (Exception e) {
			Log.e("Net", "Error in network call", e);
		}
		return m_geoData;
	}
}
